package com.apestech.framework.xml.sql.node;

import java.util.List;

import com.apestech.framework.sql.condition.ConditionMap;
import com.apestech.framework.sql.modul.FromList;
import com.apestech.framework.xml.sql.node.NodeFor;
import org.dom4j.Element;
import org.dom4j.Text;
import com.apestech.framework.sql.modul.SqlList;
import com.apestech.framework.sql.variable.SqlSection;
import com.apestech.framework.xml.pub.core.ElementUtil;

public class NodeValue extends ElementUtil {
	
	private String text;
	private String operator;
	private boolean result;
	private String expressAttrName;
	private ConditionMap input;
	private SqlList param;
	private FromList fromtab;
	
	public NodeValue(){
		this.expressAttrName="test";
	}
	
	public NodeValue(String expressAttrName){
		this.expressAttrName=expressAttrName;
	}

	public void setFromtab(FromList fromtab) {
		this.fromtab = fromtab;
	}

	public SqlList getParam(){
		return this.param;
	}
	
	public void doExpress(Element node, ConditionMap input) throws Exception{
		this.input=input;
		this.operator=node.getName();
		this.param=new SqlList();
		String test=node.attributeValue(this.expressAttrName);
		if(test==null){
			test=node.attributeValue(this.expressAttrName.toUpperCase());
		}
		this.result=false;
		if(test!=null){
			this.result=(boolean) doExpression(test, false, input.getSingle());
		}
		if(this.result){
			getNodeText(node);
		}
	}
	
	public String getText(){
		if(this.result){
			return this.text;
		}
		return "";
	}
	
	public String getOperator(){
		return this.operator;
	}	
	
	/**
	 * 获取大数据量的内容，结点体现方式属性（逻辑公式）+文本内容:
	 * 公式 express
	 * SQL语句 (select, insert, delete, create, procedure)
	 * 备注类document
	 * 代码脚本(java, script)
	 * @return
	 * @throws Exception 
	 */
	private void getNodeText(Element node) throws Exception{
		this.text="";
		if(node!=null){
			String str="";
			List<?> list=node.content();
			int len=list.size();
			for(int i=0; i<len; i++){
				String scalss=list.get(i).getClass().getName();
				if(scalss.equals("org.dom4j.tree.DefaultText")){
	        		Text text=(Text) list.get(i);
	        		String s=text.getText().trim();
	        		if(!"".equals(s)){
	        			SqlSection sqlvar=new SqlSection(fromtab, input);
	        			s=" "+s+" ";
	        			sqlvar.doVar(s);
	        			this.text=this.text+s;
	        			this.param.add(sqlvar.getParam());
	        		}
				} else if(scalss.equals("org.dom4j.tree.DefaultElement")){
					Element eml=(Element) list.get(i);
					String nname=eml.getName();
					if("if".equals(nname)){
						execIf(eml, this.input);
					} else {
						execForeach(eml, this.input);
					}
	        	}
			}
		}
	}
	
	private void execIf(Element node, ConditionMap input) throws Exception{
		NodeValue value=getNodeValue();
		value.doExpress(node, input);
		this.text=this.text+value.getText();
		this.param.add(value.getParam());
	}

	private void execForeach(Element node, ConditionMap input) throws Exception{
		NodeFor nodeFor=new NodeFor(node);
		nodeFor.setFromtab(fromtab);
		nodeFor.doExpress(input);
		if(!"".equals(nodeFor.getText())){
			this.text=this.text+nodeFor.getText()+"";
			this.param.add(nodeFor.getParam());
		}
	}

	private NodeValue getNodeValue(){
		NodeValue value;
		if(this.expressAttrName!=null){
			value=new NodeValue(this.expressAttrName);
		} else {
			value=new NodeValue();
		}
		value.setFromtab(fromtab);
		return value;
	}

}
